package com.colorful.insuranceproject.base;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.adapter.CityListAdapter;
import com.colorful.insuranceproject.adapter.CityListBean;
import com.colorful.insuranceproject.consts.CodeTable;
import com.colorful.insuranceproject.request.ClaimsRequest;
import com.lovely3x.common.utils.Response;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by handsome-Bob on 2017/10/27.
 * <p>
 * 城市选择页面
 */
public class CityChooseActivity extends TitleActivity implements AdapterView.OnItemClickListener {

    private static final int GET_CITY_LIST_DATA_URL = 0X1;

    public static final String EXTRA_POSITION = "extra_position";

    ClaimsRequest claimsRequest;

    private CityListAdapter cityListAdapter;

    @Bind(R.id.lv_city)
    ListView lv;

    @Bind(R.id.tv_now_city)
    TextView tvNowCity;

    @Bind(R.id.tv_now_city_click)
    TextView tvNowCityClick;

    CityListBean cb;

    @Override
    protected int getContentView() {
        return R.layout.activity_city_choose;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        setTitle(getString(R.string.address_select));
    }

    @Override
    protected void onInitExtras(@NonNull Bundle bundle) {
        super.onInitExtras(bundle);
        cb = bundle.getParcelable(EXTRA_POSITION);
    }

    @Override
    protected void onViewInitialized() {
        tvNowCity.setText(getString(R.string.now_city, cb.getAreaName()));
        tvNowCityClick.setText(cb.getNowCity());
        claimsRequest = new ClaimsRequest(getHandler());
        cityListAdapter = new CityListAdapter(null, mActivity);
        lv.setAdapter(cityListAdapter);
        lv.setOnItemClickListener(this);
        claimsRequest.getCityList(cb.getNowCityId(), GET_CITY_LIST_DATA_URL);
    }

    @Override
    protected void handleResponseMessage(Message msg, Response response) {
        super.handleResponseMessage(msg, response);
        switch (msg.what) {
            case GET_CITY_LIST_DATA_URL:
                if (response.isSuccessful) {
                    cityListAdapter.setData((List<CityListBean>) response.obj);
                } else {
                    showToast(CodeTable.getInstance().getCodeDescription(response));
                }
                break;
        }
    }

    @Override
    public void restoreInstanceOnCreateBefore(@NonNull Bundle savedInstance) {

    }

    @Override
    public void restoreInstanceOnCreateAfter(@NonNull Bundle savedInstance) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        CityListBean bean = (CityListBean) parent.getAdapter().getItem(position);
        cb.setAreaId(bean.getAreaId());
        cb.setAreaName(bean.getAreaName());
        cb.setIsChild(1);
        Intent intent = new Intent();
        intent.putExtra("id", cb);
        setResult(RESULT_OK, intent);
        finish();
    }

    @OnClick(R.id.tv_now_city_click)
    void onClicked() {
        cb.setIsChild(0);
        Intent intent = new Intent();
        intent.putExtra("id", cb);
        setResult(RESULT_OK, intent);
        finish();
    }
}
